package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;

    //处理查询所有员工的请求 /employee/list
    @RequestMapping("/list")
    @RequiredPermission(name="员工列表",expression="employee.list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo){
        PageInfo<Employee> pageInfo = employeeService.query(qo);//查出数据
        model.addAttribute("pageInfo", pageInfo);
        //查到所有的部门数据存到Model中
        model.addAttribute("departments", departmentService.listAll());

        //model拿到数据后与jsp合并渲染成HTML格式，展现在页面上
        return "employee/list";
    }

    //处理删除请求 /employee/delete?id=1
    @RequestMapping("/delete")

    public String delete(Long id){
        if (id!=null) {
            employeeService.delete(id);
        }
        return "redirect:/employee/list";  //localhost/employee/list
    }

    //处理去新增或者修改请求 /employee/input[?id]
    @RequestMapping("/input")
    @RequiredPermission(name="员工编辑页面",expression="employee.input")
    public String input(Long id,Model model){

        //查到所有的部门数据存到Model中
        model.addAttribute("departments",departmentService.listAll());
        model.addAttribute("allRoles",roleService.selectAll());
        if (id!=null) {
            model.addAttribute("employee",employeeService.get(id));
            model.addAttribute("selfRoles",roleService.selectByEmpId(id));
        }
        return "employee/input";
    }

    //处理保存或者修改请求请求 /employee/saveOrUpdate  //参数在请求体中，操作input界面的提交功能
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name="员工新增/更新",expression="employee.saveOrUpdate")
    public String saveOrUpdate(Employee employee,Long[] ids){
        if (employee.getId()==null) {
            employeeService.save(employee,ids);
        }else{
            employeeService.update(employee,ids);
        }
        return "redirect:/employee/list";  //localhost/employee/list
    }


    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        String fileName = new String("员工通讯录.xls".getBytes("UTF-8"), "ISO-8859-1");
        response.setHeader("Content-Disposition","attachment;filename="+fileName);
        employeeService.exportXls(response.getOutputStream());
    }

    @RequestMapping("/importXls")
    public String importXls(MultipartFile file) throws IOException {
        employeeService.importXls(file);
        return "redirect:/employee/list";
    }
}
